﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;

namespace Rappelz.GameServer
{
    public class ItemCollector : BaseObject
    {
        /// <summary>
        /// The instance!
        /// </summary>
        protected static ItemCollector m_instance;

        /// <summary>
        /// Returns the instance
        /// </summary>
        public static ItemCollector Instance
        {
            get { return m_instance; }
        }

// 
//         Data           :   static, [00000000][0000:00000000], Static Member, Type: const unsigned int, IDX_RESERVED_TO_DELETE
        public static bool Init()
        {
            ItemCollector.m_instance = new ItemCollector();
            RappelzServer.Instance.SetObjectPriority(ItemCollector.m_instance, SchedulerObject.Priority.Low);
            return true;
        }
//         Function       :   public bool ItemCollector::DeInit()

        public void RegisterItem(Item pItem)
        {
            lock(this)
            {
                this.m_vItem.Add(pItem);
                pItem.m_unInventoryIndex = (uint)(this.m_vItem.Count - 1);
            }
        }

        public bool UnregisterItem(Item pItem)
        {
            lock(this)
            {
                if ((int)pItem.m_unInventoryIndex == -1 )
                {
                    return false;
                }

                for(int i = this.m_vItem.Count-1; i >= 0; --i)
                {
                    Item item = this.m_vItem[i];
                    if(item != null && item.IsSameObject(pItem))
                    {
                        this.m_vItem.RemoveAt(i);
                        return true;
                    }
                }

                Globals.Log.Error("ItemCollector.UnregisterItem: Trying to unregister item not registered detected: Code(%d) Count(%I64d) GenCode(%d)\n",pItem.m_Instance.Code,
                    pItem.m_Instance.nCount, (int)pItem.m_Instance.GenerateInfo);
            }
            return false;
        }


//         Function       :   public static struct ItemCollector & ItemCollector::Instance()
        public override void onProcess(int nThreadIdx)
        {
            uint nIndex = 0;

            lock(this)
            {
                uint ct = Globals.GetArTime();

                for(int i = this.m_vItem.Count - 1; i >= 0; --i)
                {
                    Item item = this.m_vItem[i];
                    if (item.m_nDropTime + GameRule.nItemHold > ct || item.m_bIsEventDrop || !item.IsDeleteable())
                    {
                        ++nIndex;
                    }
                    else
                    {
                        item.m_unInventoryIndex = (uint)0xffffffff;
                        this.m_vItem.RemoveAt(i);
                        this.m_vTmp.Add(item);
                    }
                }

                foreach (Item item in this.m_vTmp)
                {
                    RLock rl = RappelzServer.Instance._LockObjectWithVisibleRange(item);
                    AutoLock al = new AutoLock(rl.handle);
                    if (item.bIsInWorld)
                    {
                        if (item.m_Instance.Code > 0 )
                        {
                            ServerLog.Log((ushort)0x96D, 0, 0, item.m_Instance.nLevel + 100 * item.m_Instance.nEnhance, item.m_Instance.Code, item.m_Instance.nCount, item.m_Instance.nCount,
                                0,0,(long)item.mv.x,(long)item.mv.y,item.m_Instance.UID,"","","SUCS","COLLECTOR");
                        }
                        RappelzServer.Instance.RemoveObject(item);
                        Item.PendFreeItem(item);
                    }
                }
            }

        }
//         Function       :   public void ItemCollector(const struct ItemCollector &)
//         Function       :   protected void ItemCollector::ItemCollector()
//         Data           :   this+0x40, Member, Type: class XCriticalSection, m_lock
//         UserDefinedType:     XCriticalSection
// 
        public List<Item> m_vItem = new List<Item>();                       // 0x80
        public List<Item> m_vTmp = new List<Item>();                        // 0x90
    }
}
